package com.twh.www.service.login.impl;

import com.twh.www.entity.WnUser;
import com.twh.www.mapper.WnUserMapper;
import com.twh.www.service.login.LoginStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

/**
 * 用户名密码登录适配器
 */
@Component
public class PasswordLoginAdapter implements LoginStrategy {
    @Autowired
    private WnUserMapper userMapper;

    @Override
    public WnUser login(Object... credentials) {
        if (credentials.length < 2) {
            return null;
        }
        String account = (String) credentials[0];
        String password = (String) credentials[1];

        // 查找用户
        WnUser user = userMapper.getUserByAccount(account);
        if (user == null) {
            return null;
        }

        // 验证密码
        String encryptedPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        if (user.getPassword().equals(encryptedPassword)) {
            return user;
        }
        return null;
    }

    @Override
    public String getLoginType() {
        return "PASSWORD";
    }
} 